/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package info.toegepaste.www.dataaccess;
import info.toegepaste.www.beans.Alumnus;
import info.toegepaste.www.beans.Event;
import java.sql.*;
import java.util.*;
import java.math.*;
/**
 *
 * @author Logic
 */
public class DAEvent {
    private Connection connection = null;

    public DAEvent(String url, String login, String password) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(url, login, password);
    }
    public DAEvent(Connection conn){
        connection = conn;
    }
    public void close() throws SQLException {
        if (connection != null) {
            connection.close();
        }
    }
        public Event getEvent(int id) {
        Event event = new Event();
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            statement = connection.prepareStatement("SELECT  * FROM event where eventID = ?");
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                event.setEventID(resultSet.getInt("eventID"));
                event.setEventnaam(resultSet.getString("eventnaam"));
                event.setEventBeschrijving(resultSet.getString("eventbeschrijving"));
                event.setPlaats(resultSet.getString("plaats"));
                event.setDatumEvent(resultSet.getDate("datumevent"));
                event.setEindeInschrijvingdatum(resultSet.getDate("eindeInschrijvingdatum"));
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
            }
        }
        return event;
    }
        public ArrayList<Event> getAlleEvents() {
       
        ArrayList<Event> resultaat = new ArrayList<Event>();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.prepareStatement("select * from event");
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
            
                Event event = new Event();
                event.setEventID(resultSet.getInt("event.eventID"));
                event.setEventnaam(resultSet.getString("event.eventnaam"));
                event.setEventBeschrijving(resultSet.getString("event.eventbeschrijving"));
                event.setPlaats(resultSet.getString("event.plaats"));
                event.setDatumEvent(resultSet.getDate("event.datumevent"));
                event.setEindeInschrijvingdatum(resultSet.getDate("event.eindeInschrijvingdatum"));
               
                
                resultaat.add(event);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                if (resultSet != null) {
                    resultSet.close();
                }
            } catch (SQLException e) {
            }
        }
        return resultaat;
    }
        public boolean insertEvent(String eventnaam, String eventbeschrijving, String plaats, java.sql.Date datumevent, java.sql.Date eindeinschrijvingdatum) {
        boolean resultaat = true;
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement("insert into event(eventnaam,eventbeschrijving,plaats,datumevent, eindeInschrijvingdatum) values(?, ?, ?, ?, ?)");
            statement.setString(1, eventnaam);
            statement.setString(2, eventbeschrijving);
            statement.setString(3, plaats);
            statement.setDate(4, datumevent);
            statement.setDate(5, eindeinschrijvingdatum);
            statement.executeUpdate();
            
        } catch (Exception e) {
            resultaat = false;
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
            }
        }
        return resultaat;
    }
}
